package com.app.twth.ui.news;


import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.RadioGroup;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.app.Constant;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityNewsBinding;
import com.app.twth.databinding.DialogTallyCheckBinding;
import com.app.twth.ui.adjust.recheck.ReCheck;
import com.app.twth.ui.adjust.redamage.destroy.RDDestroyActivity;
import com.app.twth.ui.adjust.redamage.newsku.RDNewSkuActivity;
import com.app.twth.ui.adjust.redamage.oldsku.RDOldSkuActivity;
import com.app.twth.ui.adjust.remeasure.ReMeasureActivity;
import com.app.twth.ui.adjust.retallytask.ReTallyTaskActivity;
import com.app.twth.ui.finalprocess.trk.FinalProcessTrkActivity;
import com.app.twth.ui.tally.Tally;
import com.app.twth.ui.tally.sku.TallySkuActivity;
import com.app.twth.utils.DividerItemDecoration;
import com.app.twth.utils.MyDialogUtil;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

/**
 * @describe :
 */
public class NewsActivity extends BaseActivity<ActivityNewsBinding, NewsViewModel> implements OnRefreshListener {
    private String TAG = getClass().getSimpleName();
    private int type = 0;
    private NewsAdapter adapter;

    @Override
    protected void initListener() {
        viewModel.lpnItemInfoLiveData.observe(this, data -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("FinalProcess", data);
            AppRouter.showActivityForResult(mContext, FinalProcessTrkActivity.class, bundle, Constant.ForResult2);
        });

        viewModel.reDamageLiveData.observe(this, data -> {
            Bundle bundle1 = new Bundle();
            bundle1.putSerializable("data", data);
            switch (data.getType()) {//4, "破损-销毁" 5,"破损-上架" 6,"破损-换sku上架"
                case 4:
                    AppRouter.showFragmentActivity(mContext, RDDestroyActivity.class, bundle1);
                    break;
                case 5:
                    AppRouter.showFragmentActivity(mContext, RDOldSkuActivity.class, bundle1);
                    break;
                case 6:
                    AppRouter.showFragmentActivity(mContext, RDNewSkuActivity.class, bundle1);
                    break;
            }
        });
        viewModel.reCheckLiveData.observe(this, data -> {
            showCheckDialog(data);
        });
        viewModel.reTallyLiveData.observe(this, data -> {
            Log.e(TAG, "ReTallyTaskActivity");
            Bundle bundle1 = new Bundle();
            bundle1.putSerializable("data", data);
            AppRouter.showFragmentActivity(mContext, ReTallyTaskActivity.class, bundle1);
        });
        viewModel.reMeasureLiveData.observe(this, data -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("data", data);
            AppRouter.showFragmentActivity(mContext, ReMeasureActivity.class, bundle);
        });

        viewModel.newsLiveData.observe(this, data -> {
            //关闭下拉
            binding.refreshLayout.finishRefresh();
            adapter.setData(data, type);
        });
        viewModel.errorResultCode.observe(this, code -> {
            //关闭下拉
            binding.refreshLayout.finishRefresh();
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return "News";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setLayoutBottomContentGone();
        binding.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_pending:
                        type = 0;
                        setLine();
                        break;
                    case R.id.rb_done:
                        type = 1;
                        setLine();
                        break;
                }
            }
        });

        adapter = new NewsAdapter(mContext, viewModel.newsLiveData.getValue(), position -> {
            if (type == 1) {//done状态无须操作
                return;
            }
            if (viewModel.newsLiveData.getValue().size() <= position) {
                return;
            }
            NewsBean newsBean = viewModel.newsLiveData.getValue().get(position);
            if (newsBean == null) {
                return;
            }
            try {
                switch (newsBean.getType()) {//任务类型（1:Re-Tally 2:Re-Measure 3:Re-Check）4, "破损-销毁" 5,"破损-上架" 6,"破损-换sku上架"
                    case 1:
                        viewModel.tallyReTally(newsBean);
                        break;
                    case 2:
                        viewModel.tallyReMeasure(newsBean);
                        break;
                    case 3:
                        viewModel.tallyReCheckInfo(newsBean);
                        break;
                    case 4:
                    case 5:
                    case 6:
                        viewModel.damageProcessInfo(newsBean);
                        break;
                    case 7:
                        viewModel.returnFinalProcessItem(newsBean);
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));
        binding.recyclerView.setAdapter(adapter);

        binding.refreshLayout.setOnRefreshListener(this);
    }

    private void setLine() {
        binding.viewPending.setBackgroundColor(type == 0 ? getResources().getColor(R.color.colorPrimary) : getResources().getColor(R.color.color_cccccc));
        binding.viewDone.setBackgroundColor(type == 1 ? getResources().getColor(R.color.colorPrimary) : getResources().getColor(R.color.color_cccccc));
        adapter.setData(new ArrayList<>(), 0);
        binding.refreshLayout.autoRefresh();//触发自动刷新
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bottomLayoutBinding.tvHint.setText("");
        binding.refreshLayout.autoRefresh();//触发自动刷新
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
        }
    }

    @Override
    public void getScanninCode() {
        super.getScanninCode();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        switch (type) {
            case 0:
                viewModel.taskPendingList();
                break;
            case 1:
                viewModel.taskPendingList();
                break;

        }
    }

    private AlertDialog dialogLogout;

    private void showCheckDialog(ReCheck reCheck) {
        dialogLogout = MyDialogUtil.getDialog(mContext, initCheckDialogView(reCheck), Gravity.CENTER);
        dialogLogout.setCanceledOnTouchOutside(true);
        dialogLogout.show();
    }

    private View initCheckDialogView(ReCheck reCheck) {
        DialogTallyCheckBinding dialogBinding = DialogTallyCheckBinding.inflate(getLayoutInflater());

        dialogBinding.tvContainerNo.setText(reCheck.getContainerNo());
        dialogBinding.tvSku.setText(reCheck.getSku());
        dialogBinding.tvPlanQuantity.setText(reCheck.getPlanQty() + "");
        dialogBinding.tvLatestTallyQuantity.setText(reCheck.getLatestTalliedQty() + "");

//        dialogBinding.tvCancel.setVisibility(View.VISIBLE);
//        dialogBinding.viewLine.setVisibility(View.VISIBLE);
//        dialogBinding.tvCancel.setOnClickListener(v -> {
//            if (dialogLogout != null) {
//                dialogLogout.dismiss();
//            }
//        });
//        dialogBinding.tvConfirm.setText(getText(R.string.pickerview_submit));
        dialogBinding.tvConfirm.setOnClickListener(v -> {
            if (dialogLogout != null) {
                dialogLogout.dismiss();
            }
            Tally tally = new Tally();
            tally.setOrderId(reCheck.getInboundOrderId());
            tally.setContainerNo(reCheck.getContainerNo());
            tally.setSku(reCheck.getSku());
            tally.setTaskId(reCheck.getTaskId());
            Bundle bundle = new Bundle();
            bundle.putSerializable("Tally", tally);

            AppRouter.showFragmentActivity(mContext, TallySkuActivity.class, bundle);
        });
        return dialogBinding.getRoot();
    }

}
